package com.trsj.lnsj.service.imp;

import java.util.Date;
import java.util.List;

import com.trsj.lnsj.util.HttpRequestUtil;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.trsj.lnsj.common.DataResponse;
import com.trsj.lnsj.common.MsgStatus;
import com.trsj.lnsj.entity.GroupDTO;
import com.trsj.lnsj.entity.GroupRepo;
import com.trsj.lnsj.entity.NoticeDTO;
import com.trsj.lnsj.entity.NoticeRepo;
import com.trsj.lnsj.entity.NoticeStatusDTO;
import com.trsj.lnsj.entity.NoticeStatusRepo;
import com.trsj.lnsj.util.CommTool;
import com.trsj.lnsj.util.ResponseHelper;

/**
 * 
 * <p>
 * Title: GroupService.java
 * </p>
 * 
 * <p>
 * Description:
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2018
 * </p>
 * 
 * <p>
 * Company: 七色环
 * </p>
 * 
 * @author longky
 * 
 * @date 2018年8月30日
 * 
 * @version 1.0
 * 
 */
@Service
public class GroupService {
    private Logger  logger = LoggerFactory.getLogger(GroupService.class);
    @Autowired
    private GroupRepo groupRepo;
    @Autowired
    private NoticeRepo noticeRepo;
    @Autowired
    private NoticeStatusRepo noticeStatusRepo;

    public GroupDTO addGroup(GroupDTO group) {
        return groupRepo.save(group);
    }

    public Page<GroupDTO> listGroup(Pageable pageable) {
        return groupRepo.findAll(pageable);
    }

    public DataResponse listNotice(PageRequest pageRequest, long gid) {
        DataResponse resp = null;
        try {
            Page<NoticeDTO> page = noticeRepo.findByGid(pageRequest, gid);
            resp = ResponseHelper.getSuccessResponse(page);
        } catch (Exception e) {
            logger.info(e.getMessage());
            resp = ResponseHelper.getResponse(MsgStatus.STATUS_FAILED);
        }
        return resp;
    }

    public DataResponse noticeSave(NoticeDTO notice) {
        DataResponse resp = null; 
        try {
            List<NoticeDTO> existList = noticeRepo.findByTitleAndGid(notice.getTitle(), notice.getGid());
            if (CollectionUtils.isNotEmpty(existList)) {
                resp = ResponseHelper.getResponse(MsgStatus.DATA_REPEATED);
            } else {
                if (null == notice.getId()) {
                    notice.setCreateTime(new Date());
                    notice.setUpdateTime(new Date());
                    noticeRepo.saveAndFlush(notice);
                } else {
                    NoticeDTO dbObj = noticeRepo.findOne(notice.getId());
                    CommTool.copyPropertiesNotForce(dbObj, notice);
                    notice.setUpdateTime(new Date());
                    noticeRepo.saveAndFlush(dbObj);
                }
                resp = ResponseHelper.getResponse(MsgStatus.STATUS_OK);
            }
        } catch (Exception e) {
            logger.info(e.getMessage());
            resp = ResponseHelper.getResponse(MsgStatus.STATUS_FAILED);
        }
        return resp;
    }

    public DataResponse noticeDel(int id) {
        DataResponse resp = null; 
        try {
            noticeRepo.delete(id);;
            resp = ResponseHelper.getResponse(MsgStatus.STATUS_OK);
        } catch (Exception e) {
            logger.info(e.getMessage());
            resp = ResponseHelper.getResponse(MsgStatus.STATUS_FAILED);
        }
        return resp;
    }

    public DataResponse noticeRead(NoticeStatusDTO ns) {
        DataResponse resp = null;
        ns.setStatus((short)1);
        try {
            NoticeStatusDTO nsdto = noticeStatusRepo.saveAndFlush(ns);
            resp = new DataResponse(MsgStatus.STATUS_OK,nsdto);
        } catch (Exception e) {
            logger.info(e.getMessage());
            resp = ResponseHelper.getResponse(MsgStatus.STATUS_FAILED);
        }
        return resp;
    }
}
